﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Business;

namespace WorkOrder.workBill
{
    public partial class timely : System.Web.UI.Page
    {
        TaskReport tr = new TaskReport();
        private static DataTable dt;
        private static int tmp = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int a, b, _e, f, g, h, i;
                a = tr.getSum(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1",
                              DateTime.Now.Date.ToShortDateString());
                b = tr.getCountTimeOut(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1",
                                       DateTime.Now.Date.ToShortDateString());
                _e = tr.getConfirmOnLine();
                f = tr.getConfirmOnlineTimeOut();
                g = tr.getConfirmOnlineAlarm30();
                h = tr.getConfirmOnlineAlarm5();
                i = tr.getConfirmOnlineAlarm10();
                LinkButtonA.Text = a.ToString();
                LinkButtonB.Text = b.ToString();
                if (a != 0) lblC.Text = Math.Round(Math.Round((decimal)(a - b) / (decimal)a, 4) * 100, 2).ToString();
                lblD.Text = tr.getOnline().ToString();
                LinkButtonE.Text = _e.ToString();
                LinkButtonF.Text = f.ToString();
                LinkButtonG.Text = g.ToString();
                LinkButtonH.Text = h.ToString();
                LinkButtonI.Text = i.ToString();

                dt = null;
            }
        }

        #region 处理光标行颜色事件
        protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            try
            {
                if (Convert.ToDouble(e.Row.Cells[5].Text) < 0)
                    e.Row.Cells[5].BackColor = System.Drawing.Color.Coral;
                if (Convert.ToDouble(e.Row.Cells[5].Text) > 0 && Convert.ToDouble(e.Row.Cells[5].Text) < 10)
                    e.Row.Cells[5].BackColor = System.Drawing.Color.Yellow;
                if (e.Row.Cells[3].Text=="是")
                    e.Row.Cells[3].BackColor = System.Drawing.Color.Coral;
            }
            catch { }  
        }
        protected void gvReport2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            try
            {
                if (Convert.ToDouble(e.Row.Cells[4].Text) > 0)
                    e.Row.Cells[4].BackColor = System.Drawing.Color.Coral;
                if (e.Row.Cells[2].Text == "是")
                    e.Row.Cells[2].BackColor = System.Drawing.Color.Coral;
            }
            catch { }
        }
        #endregion

        protected void LinkButtonE_Click(object sender, EventArgs e)
        {
            tmp = 3;
            GridView1.Visible = true;
            GridView2.Visible = false;
            dt = tr.getConfirmOnLineList("");
            GridView1.DataSource = dt;
            GridView1.DataBind();
            
        }
        
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string tmpStr = "";
            if (tmp == 1)
            {
                tmpStr = "已完成工单";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 2)
            {
                tmpStr = "已完成的超时工单";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 3)
            {
                tmpStr = "售中环节在线工单";
                string[] title = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 4)
            {
                tmpStr = "售中环节在线工单超时";
                string[] title = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 5)
            {
                tmpStr = "专线售中环节在线超时30个工作日";
                string[] title = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 6)
            {
                tmpStr = "非专线售中环节在线超时5个工作日";
                string[] title = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 7)
            {
                tmpStr = "专线取消单售中环节在线超时10个工作日";
                string[] title = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "工单状态", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string tmpStr = "";
            if (tmp == 1)
            {
                tmpStr = "已完成工单";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 2)
            {
                tmpStr = "已完成的超时工单";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "超时天数", "工单历时", "售中在线历时", "派单时间", "完成时间", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 3)
            {
                tmpStr = "售中环节在线工单";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 4)
            {
                tmpStr = "售中环节在线工单超时";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 5)
            {
                tmpStr = "专线售中环节在线超时30个工作日";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 6)
            {
                tmpStr = "非专线售中环节在线超时5个工作日";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
            if (tmp == 7)
            {
                tmpStr = "专线取消单售中环节在线超时10个工作日";
                string[] title = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                string[] field = { "工单号", "工单主题", "是否超时", "超时时间", "距离超时时间", "工单历时", "售中在线历时", "派单时间", "CRM资源确认时间", "当前环节", "当前处理流程", "产品类型", "业务类型" };
                Export(title, field, tmpStr);
            }
        }

        private void Export(string[] title, string[] field, string fileName)
        {
            if (dt != null)
            {
              ExportDsToXls(this.Page, fileName, title, field, dt);
            }
        }

        public static string ExportTable(string[] title, string[] field, DataTable table)
        {
            StringBuilder sb = new StringBuilder();

            //列名
            string tmp = "";
            for (int i = 0; i < title.Length; i++)
            {
                if (i > 0) tmp += "\t";
                tmp += title[i];
            }
            sb.AppendLine(tmp);
            tmp = null; title = null;

            //数据内容
            foreach (DataRow row in table.Rows)
            {
                string str = "";

                for (int j = 0; j < field.Length; j++)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        if (column.ColumnName == field[j])
                        {
                            if (j > 0)
                                str += "\t";
                            if (DBNull.Value.Equals(row[column]))
                                str += "";
                            else if (row[column].ToString() == "False")
                                str += "否";
                            else if (row[column].ToString() == "True")
                                str += "是";
                            else str += row[column].ToString().Replace("\r", " ").Replace("\n", " ");
                        }
                    }
                }
                sb.AppendLine(str);
                str = null;
            }
            field = null;

            return sb.ToString();
        }

        public static void ExportDsToXls(Page page, string fileName, string[] title, string[] field, DataTable table)
        {
            page.Response.Clear();
            page.Response.Buffer = true;
            page.Response.Charset = "GB2312";
            //page.Response.Charset = "UTF-8";
            page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + System.DateTime.Now.ToString("_yyyyMMddHHmmss") + ".xls");
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            page.Response.ContentType = "application/vnd.ms-excel";//设置输出文件类型为excel文件。 
            page.EnableViewState = false;
            page.Response.Write(ExportTable(title, field, table));
            page.Response.End();
        }

        //protected void Timer1_Tick(object sender, EventArgs e)
        //{
        //    int a, b, _e, f, g, h, i;
        //    a = tr.getSum(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1",
        //                  DateTime.Now.Date.ToShortDateString());
        //    b = tr.getCountTimeOut(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1",
        //                           DateTime.Now.Date.ToShortDateString());
        //    _e = tr.getConfirmOnLine();
        //    f = tr.getConfirmOnlineTimeOut();
        //    g = tr.getConfirmOnlineAlarm30();
        //    h = tr.getConfirmOnlineAlarm5();
        //    i = tr.getConfirmOnlineAlarm10();
        //    LinkButtonA.Text = a.ToString();
        //    LinkButtonB.Text = b.ToString();
        //    lblC.Text = Math.Round(Math.Round((decimal)(a - b) / (decimal)a, 4) * 100, 2).ToString();
        //    lblD.Text = tr.getOnline().ToString();
        //    LinkButtonE.Text = _e.ToString();
        //    LinkButtonF.Text = f.ToString();
        //    LinkButtonG.Text = g.ToString();
        //    LinkButtonH.Text = h.ToString();
        //    LinkButtonI.Text = i.ToString();
        //}

        protected void LinkButtonF_Click(object sender, EventArgs e)
        {
            tmp = 4;
            GridView1.Visible = true; GridView2.Visible = false;
            dt = tr.getConfirmOnlineTimeOutList();
            GridView1.DataSource = dt;
            GridView1.DataBind(); 
        }

        protected void LinkButtonG_Click(object sender, EventArgs e)
        {
            tmp = 5;
            GridView1.Visible = true; GridView2.Visible = false;
            dt = tr.getConfirmOnlineAlarm30List();
            GridView1.DataSource = dt;
            GridView1.DataBind(); 
        }

        protected void LinkButtonH_Click(object sender, EventArgs e)
        {
            tmp = 6;
            GridView1.Visible = true; GridView2.Visible = false;
            dt = tr.getConfirmOnlineAlarm5List();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void LinkButtonI_Click(object sender, EventArgs e)
        {
            tmp = 7;
            GridView1.Visible = true; GridView2.Visible = false;
            dt = tr.getConfirmOnlineAlarm10List();
            GridView1.DataSource = dt;
            GridView1.DataBind(); 
        }

        protected void LinkButtonA_Click(object sender, EventArgs e)
        {
            tmp = 1; 
            GridView2.Visible = true;
            GridView1.Visible = false;
            dt = tr.getSumList(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1",
                              DateTime.Now.Date.ToShortDateString());
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

        protected void LinkButtonB_Click(object sender, EventArgs e)
        {
            tmp = 2;
            GridView2.Visible = true;
            GridView1.Visible = false;
            dt = tr.getCountTimeOutList(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "1",
                              DateTime.Now.Date.ToShortDateString());
            GridView2.DataSource = dt;
            GridView2.DataBind(); 
        }
    }
}